package model

import (
	"cangqiong-takeout/src/common"
	"github.com/golang-jwt/jwt/v5"
	"time"
)

type Employee struct {
	ID               int       `gorm:"primarykey" json:"id"`
	CreateTime       time.Time `json:"-"`
	UpdateTime       time.Time `json:"-"`
	Name             string    `gorm:"type:varchar(32)" json:"name"`                                                                                                               //姓名
	UserName         string    `gorm:"type:varchar(32);column:username" json:"username"`                                                                                           //用户名
	PassWord         string    `gorm:"type:varchar(64);column:password" json:"password"`                                                                                           //密码
	Phone            string    `gorm:"type:varchar(11)" valid:"matches(^1[3-9]{1}\\d)" json:"phone"`                                                                               //电话
	Sex              string    `gorm:"type:varchar(2)" json:"sex"`                                                                                                                 //性别
	IdNumber         string    `gorm:"type:varchar(18)" valid:"matches(^[1-9]\\d{5}(?:18|19|20)\\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\\d|30|31)\\d{3}[\\dXx]$)" json:"idNumber"` //身份证 正则匹配第二代大陆身份证
	Status           int8      `json:"status"`                                                                                                                                     //账号状态 1正常 0锁定
	CreateUser       int       `json:"createUser"`                                                                                                                                 //创建人id
	UpdateUser       int       `json:"updateUser"`                                                                                                                                 //最后修改人id
	FormatUpdateTime string    `gorm:"-" json:"updateTime"`
	FormatCreateTime string    `gorm:"-" json:"createTime"`
}

func (*Employee) TableName() string {
	return "employee"
}
func (e *Employee) FormatUpdateTimeFunc() {
	e.FormatUpdateTime = FormatTime(e.UpdateTime)
}
func (e *Employee) FormatCreateTimeFunc() {
	e.FormatUpdateTime = FormatTime(e.CreateTime)
}
func (e *Employee) EncryptionPwd(replace string) {
	e.PassWord = replace
}
func FormatTime(t time.Time) string {
	return t.Format("2006-01-02 15:04:05")
}

type EmployeeUserJwt struct {
	ID       int
	Name     string
	PassWord string
	jwt.RegisteredClaims
}

type takeWayT[T any] interface {
}
type TakeWayModelT[T any] interface {
	~int | ~float64 | ~string | takeWayT[T]
}

type Category[T TakeWayModelT[T]] struct {
	ID               int `gorm:"primarykey" json:"id"`
	CreateTime       time.Time
	UpdateTime       time.Time
	Name             string `gorm:"type:varchar(32)" json:"name"`
	Type             T      `json:"type"`
	Sort             T      `json:"sort"`
	Status           int8   `json:"status"`
	CreateUser       int    `json:"createUser"` //创建人id
	UpdateUser       int    `json:"updateUser"` //最后修改人id
	FormatCreateTime string `gorm:"-" json:"createTime"`
	FormatUpdateTime string `gorm:"-" json:"updateTime"`
}

func (*Category[T]) TableName() string {
	return "category"
}
func (em *Category[T]) forMatTimeToString(etime time.Time) string {
	return etime.Format("2006-01-02 15:04:05")
}

func (em *Category[T]) SetStringToUpdateTime() {
	em.FormatUpdateTime = em.forMatTimeToString(em.UpdateTime)
}
func (em *Category[T]) SetStringToCreateTime() {
	em.FormatCreateTime = em.forMatTimeToString(em.CreateTime)
}

type Dish[T TakeWayModelT[T]] struct {
	ID               int          `gorm:"primarykey" json:"id"`
	Name             string       `gorm:"type:varchar(32)" json:"name"`
	CategoryID       int          `json:"categoryId"`
	Price            T            `gorm:"type:decimal(10,2)" json:"price"`
	Image            string       `gorm:"type:varchar(255)" json:"image"`
	Description      string       `gorm:"type:varchar(255)" json:"description"`
	Status           int8         `json:"status"`
	CreateUser       int          `json:"-"` //创建人id
	UpdateUser       int          `json:"-"` //最后修改人id
	UpdateTime       time.Time    `json:"-"`
	CreateTime       time.Time    `json:"-"`
	FormatCreateTime string       `gorm:"-" json:"createTime"`
	FormatUpdateTime string       `gorm:"-" json:"updateTime"`
	Flavors          []DishFlavor `gorm:"-" json:"flavors"`
	CategoryName     string       `gorm:"-" json:"categoryName"`
}

func (*Dish[T]) TableName() string {
	return "dish"
}

func (dish *Dish[T]) SetStringToUpdateTime() {
	dish.FormatUpdateTime = FormatTime(dish.UpdateTime)
}
func (dish *Dish[T]) SetStringToCreateTime() {
	dish.FormatCreateTime = FormatTime(dish.CreateTime)
}
func (dish *Dish[T]) SetImage(name string) {
	dish.Image = common.ImgURL + name
}

type DishFlavor struct {
	ID     uint   `gorm:"primarykey" json:"id"`
	DishID uint   `json:"dishId"`
	Name   string `gorm:"type:varchar(32)" json:"name"`
	Value  string `gorm:"type:varchar(255)" json:"value"`
}

func (*DishFlavor) TableName() string {
	return "dish_flavor"
}
